package com.cqu.hospitalsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqu.hospitalsystem.Vo.Check;
import com.cqu.hospitalsystem.Vo.CheckNew;
import com.cqu.hospitalsystem.Vo.preCheck;
import com.cqu.hospitalsystem.entity.Checkitem;
import com.cqu.hospitalsystem.entity.Checklist;
import com.cqu.hospitalsystem.entity.Patient;
import com.cqu.hospitalsystem.entity.Prescription;
import com.cqu.hospitalsystem.service.ChecklistService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("checklist")
@Tag(name = "ChecklistController", description = "检查单管理")
public class ChecklistController {
    /**
     * 服务对象
     */
    @Resource
    private ChecklistService checklistService;


    @PostMapping("saveChecklist")
    @Operation(summary = "保存检查单", description = "根据检查单对象保存检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "保存成功"),
            @ApiResponse(responseCode = "400", description = "保存失败")
    })
    public DataResult saveChecklist(@RequestBody CheckNew checkNew) throws ParseException {

        Long regId = checkNew.getRegId();
        List<Long> clist = checkNew.getCheckitemList();
        List<String> cplist = checkNew.getChcekPartList();

        Check check = new Check();
        check.setCheckitemList(clist);
        check.setRegId(regId);
        check.setChcekPartList(cplist);

        System.out.println("saveChecklist");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.checklistService.saveChecklist(check));
        System.out.println(jsonObject);
        return DataResult.successByData(jsonObject);
    }

    /**
     * 分页查询
     *
     * @param checklist   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询检查单", description = "根据筛选条件和分页信息查询检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<Checklist>> queryByPage(Checklist checklist, PageRequest pageRequest) {
        return ResponseEntity.ok(this.checklistService.queryByPage(checklist, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "根据ID查询检查单", description = "根据检查单ID查询单个检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "检查单不存在")
    })
    public ResponseEntity<Checklist> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.checklistService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param checklist 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增检查单", description = "创建新的检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<Checklist> add(Checklist checklist) {
        return ResponseEntity.ok(this.checklistService.insert(checklist));
    }

    /**
     * 编辑数据
     *
     * @param checklist 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑检查单", description = "更新检查单信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<Checklist> edit(Checklist checklist) {
        return ResponseEntity.ok(this.checklistService.update(checklist));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @Operation(summary = "删除检查单", description = "根据检查单ID删除检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.checklistService.deleteById(id));
    }

    /**
     * 查看当天检查单
     *
     * @param patient
     * @return
     */
    @PostMapping("showPaidCheck")
    public DataResult showPaidCheck(@RequestBody Patient patient) {

        List<preCheck> paidChecks = this.checklistService.showPaidCheck(patient);
        if (paidChecks.size() != 0) {
            return DataResult.successByDataArray(paidChecks);
        } else {
            return DataResult.successByData(null);
        }
    }

    /**
     * 查看历史检查单
     */
    @PostMapping("showCheck")
    @Operation(summary = "查看当天已支付检查单", description = "根据患者对象查看当天已支付的检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "无检查单")
    })
    public DataResult showCheck(@RequestBody Checklist checklist) {
//        System.out.println("查看检查单");
        System.out.println(checklist.getChecklistId());
        return DataResult.successByData(this.checklistService.showCheck(checklist.getChecklistId()));
    }

    /**
     * 历史检查单项目列表
     *
     * @param checklist
     * @return
     */
    @PostMapping("showHistoryCheck")
    @Operation(summary = "查看历史检查单项目列表", description = "根据检查单ID查看历史检查单项目列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "无历史检查单")
    })
    public DataResult showHistoryCheck(@RequestBody Checklist checklist) {
        System.out.println("历史进入检查单列表：" + checklist.getChecklistId());
        Double totalPrice = 0.0;
        List<preCheck> zww = this.checklistService.showHistoryCheck(checklist.getChecklistId());
        for (preCheck item : zww) {
            if (item.getState() != 3) {
                totalPrice = totalPrice + item.getCheckitemPrice();
                item.setTotalPrice(totalPrice);
            }
        }
        if (zww.size() != 0) {
            zww.get(0).setTotalPrice(totalPrice);
        }
        return DataResult.successByDataArray(zww);
    }

    /**
     * 未缴费检查单
     *
     * @param checklist
     * @return
     */
    @PostMapping("showUnpaidCheck")
    @Operation(summary = "查看未缴费检查单", description = "根据检查单ID查看未缴费检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "无未缴费检查单")
    })
    public DataResult showUnpaidCheck(@RequestBody Checklist checklist) {
        System.out.println("未缴费检查单列表：" + checklist.getChecklistId());
        Double totalPrice = 0.0;
        List<preCheck> zww = this.checklistService.showUnpaidCheck(checklist.getChecklistId());
        for (preCheck item : zww) {
            totalPrice = totalPrice + item.getCheckitemPrice();
            item.setTotalPrice(totalPrice);
        }
        if (zww.size() != 0) {
            zww.get(0).setTotalPrice(totalPrice);
        }
        return DataResult.successByDataArray(zww);

    }

    /**
     * 支付检查单
     *
     * @param checklist
     * @return
     */
    @PostMapping("payCheck")
    @Operation(summary = "支付检查单", description = "根据检查单ID支付检查单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "支付成功"),
            @ApiResponse(responseCode = "400", description = "支付失败")
    })
    public DataResult payCheck(@RequestBody Checklist checklist) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("isSuccess", this.checklistService.payCheck(checklist.getChecklistId()));
        return DataResult.successByData(jsonObject);

    }


}

